package org.itcast.travel.controller;



import lombok.extern.slf4j.Slf4j;
import org.itcast.travel.exception.VoteNotAllowedExcepton;
import org.itcast.travel.pojo.Race;
import org.itcast.travel.pojo.RaceQueryParam;

import org.itcast.travel.pojo.Result;
import org.itcast.travel.service.RaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@Slf4j
@RestController
@RequestMapping("/race")
public class RaceController {
    @Autowired
    private RaceService raceService;


    /**
     * 比赛报名
     * @param race 作品对象
     * @return 无返回数据
     */
    @PostMapping
    public Result enrollFor(@RequestBody Race race){
        log.info("race:{}",race);
        raceService.enrollFor(race);
        return Result.success();
    }

    /**
     *修改比赛信息
     * @param race 作品对象
     * @return 无返回值
     */
    @PutMapping
    public Result update(@RequestBody Race race){
        log.info("race:{}",race);
        raceService.update(race);
        return Result.success();
    }

    /**
     * 修改回显，根据id查询
     * @param id 活动id
     * @return 返回作品对象
     */
    @GetMapping("/{id}")
    public Result getByid(@PathVariable Integer id){
        log.info("id:{}",id);
        Race race = raceService.getByid(id);
        return Result.success(race);
    }

    /**
     * 分页查询
     * @param raceQueryParam 查询条件
     * @return 返回分页数据
     */
    @GetMapping
    public Result page(RaceQueryParam raceQueryParam){
        log.info("raceQueryParam:{}",raceQueryParam);
        return Result.success(raceService.page(raceQueryParam));
    }

    /**
     * 删除比赛信息
     * @param id 活动表id
     * @param raceId 投票表活动id
     * @return 无返回值
     */
    @DeleteMapping("/{id}/{raceId}")
    public Result delete(@PathVariable Integer id,@PathVariable Integer raceId){
        log.info("userId:{}",raceId);
        log.info("id:{}",id);
        raceService.delete(id,raceId);
        return Result.success();
    }

    /**
     * 投票
     * @param userId 用户id
     * @param raceId 活动id
     * @return 无返回值
     * @throws "VoteNotAllowedException"
     */
    @GetMapping("/{userId}/{raceId}")
    public Result vote(@PathVariable("userId") Integer userId, @PathVariable("raceId") Integer raceId) throws VoteNotAllowedExcepton {
        log.info("userId:{}",userId);
        log.info("raceId:{}",raceId);
        raceService.vote(userId,raceId);
        return Result.success();
    }

}
